/*
 * Copyright (c) 2009, Takashi Ito
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the authors nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "tssp_internal.h"
#include "tssp_dvb_dr_13.h"


int tssp_dr_carousel_identifier_parse(struct tssp_dr_carousel_identifier *dest,
                                      const struct tssp_descriptor *src)
{
    TSSP_ASSERT(dest);
    TSSP_ASSERT(src);

    if (src->tag != 0x13)
        return TSSP_STATUS_FAILED;

    if (src->length < 5)
        return TSSP_STATUS_FAILED;

    dest->carousel_id = TSSP_TO_UINT32(src->data);
    dest->FormatID = src->data[4];

    if (dest->FormatID == 0x00) {
        dest->u.standard.private_data_length = src->length - 5;
        if (src->length > 5)
            memcpy(dest->u.standard.private_data_byte,
                   &src->data[5],
                   dest->u.standard.private_data_length);
    } else if (dest->FormatID == 0x01) {
        if (src->length < 21 || (src->data[20] + 21) > src->length)
            return TSSP_STATUS_FAILED;

        dest->u.enhanced.ModuleVersion = src->data[5];
        dest->u.enhanced.ModuleId = TSSP_TO_UINT16(&src->data[6]);
        dest->u.enhanced.BlockSize = TSSP_TO_UINT16(&src->data[8]);
        dest->u.enhanced.ModuleSize = TSSP_TO_UINT32(&src->data[10]);
        dest->u.enhanced.CompressionMethod = src->data[14];
        dest->u.enhanced.OriginalSize = TSSP_TO_UINT32(&src->data[15]);
        dest->u.enhanced.TimeOut = src->data[19];
        dest->u.enhanced.ObjectKeyLength = src->data[20];

        memcpy(dest->u.enhanced.data, &src->data[21], src->length - 21);

        dest->u.enhanced.ObjectKeyData = dest->u.enhanced.data;
        dest->u.enhanced.private_data_length =
            src->length - dest->u.enhanced.ObjectKeyLength - 21;
        dest->u.enhanced.private_data_byte =
            &dest->u.enhanced.data[dest->u.enhanced.ObjectKeyLength];
    }

    return TSSP_STATUS_OK;
}

struct tssp_dr_carousel_identifier *
tssp_dr_carousel_identifier_create(const struct tssp_descriptor *src)
{
    struct tssp_dr_carousel_identifier *dest;

    dest = malloc(sizeof(*dest));
    if (dest == NULL)
        return NULL;

    if (tssp_dr_carousel_identifier_parse(dest, src) == TSSP_STATUS_OK)
        return dest;

    free(dest);
    return NULL;
}

